﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
using buildingLib;
using System.Windows.Forms;

namespace _dmis
{
    public class FtpLib
    {
        public Hashtable hashtable = new Hashtable();

        /// <summary>
        /// 上传文件功能，可以批量上传，也可以单个文件上传
        /// </summary>
        /// <param name="fileNames">所有上传文件的文件名</param>
        /// <param name="filePaths">所有上传文件的路径</param>
        /// <param name="uploadPath">存放到ftp服务器的路径</param>
        public void UploadFile(List<string> fileNames, List<string> filePaths, string uploadPath,string extend)
        {
            string uploadFilePath = "";

            if (uploadPath != "")
            {
                string[] strDirs = uploadPath.Split('_');
                string strLouhao = strDirs[0];
                char lou = (strDirs[0])[0];
                if (strDirs[1].Contains("单元"))
                {
                    char danyuan = (strDirs[1][0]);
                    uploadFilePath = strLouhao + "电子文档" + "/" + lou + "-" + danyuan + "单元" + "/" +
                        lou + "-" + danyuan + "-" + strDirs[2] + "/";
                }
                else if (strDirs[1].Contains("层"))
                {
                    if (strDirs[1].Contains("层") && strDirs[1][0] == '0')
                    {
                        char ceng = strDirs[1][1];
                        uploadFilePath = strLouhao + "电子文档" + "/" + ceng + "层" + "/" +
                            lou + "-" + ceng + "-" + strDirs[2] + "/";
                    }
                    else
                    {
                        string ceng = strDirs[1][0].ToString() + strDirs[1][1].ToString();
                        uploadFilePath = strLouhao + "电子文档" + "/" + ceng + "层" + "/" +
                            lou + "-" + ceng + "-" + strDirs[2] + "/";
                    }
                }
            }
            else
            {
                uploadFilePath = "";
            }
            for (int i = 0; i < filePaths.Count; i++)
            {
                int index = fileNames[i].LastIndexOf('.');
                string fmark = " " + System.DateTime.Now.ToFileTime().ToString();
                string ext = fileNames[i].Substring(index+1);
                string filename = "";
                switch (ext)
                {
                    case "bmp":
                        filename = fileNames[i].Substring(0, index) + fmark + ".bmp";
                        break;
                    case "jpg":
                        filename = fileNames[i].Substring(0, index) + fmark + ".jpg";
                        break;
                    case "gif":
                        filename = fileNames[i].Substring(0, index) + fmark + ".gif";
                        break;
                }
                hashtable.Add(fileNames[i].Substring(0, index), fmark);

                //从App.config中读取ftp服务器地址，登录名和密码
                string strFtp = System.Configuration.ConfigurationSettings.AppSettings["connectionFtp"].ToString();
                string[] ftpInfo = strFtp.Split(';');
                string ftpServer = "ftp://" +  ftpInfo[0].Split('=')[1] + "/";
                string ftpUser = ftpInfo[1].Split('=')[1];
                string ftpPassWord = ftpInfo[2].Split('=')[1];

                WebClient request = new WebClient();
                request.Credentials = new NetworkCredential(ftpUser, ftpPassWord);

                FileStream myStream = new FileStream(filePaths[i], FileMode.Open, FileAccess.Read);
                byte[] dataByte = new byte[myStream.Length];
                myStream.Read(dataByte, 0, dataByte.Length);  //写到2进制数组中
                myStream.Close();

                string path = ftpServer + uploadFilePath + filename;
                request.UploadData(ftpServer + uploadFilePath + filename,dataByte);
                request.Dispose();
            }
            DBHelper dbhelper = new DBHelper();
            dbhelper.UpdateRasterFileName(hashtable,extend);
            hashtable.Clear();
        }
    }
}
